<?php

//array(2) { ["s"]=> string(15) "/h5/user/detail" ["uid"]=> string(5) "88744" }

require_once 'db_mysql_im.php';

header('Content-Type: application/json');
$s = $_REQUEST["s"];

if($s == "/h5/user/detail" || $s == "/web/user/detail"){
    $uid = $_REQUEST["uid"];
    
    // echo '{"code":0,"msg":"ok","data":{"uid":88744,"username":"wangxd","nickname":"wangxd","sign":"wangxd","avatar":"\/static\/avatar.jpg","state":"online","logout_timestamp":0,"account_state":"normal","is_friend":true,"blocked":0,"remark":"","allow_friend":1,"top":0}}';
    // and to_userId != '100'
        $user = $db->select("im", "sid='$uid' GROUP BY id DESC");
    // $items = $db->select("im", "sid = '$uid' ");
    
    // var_dump($items);
    
        $items["uid"]=$user['sid'];
        $items["username"]=$user['to_userId'];
        $items["nickname"]="[".$user['owner_fishNick']."]".$user['to_fishNick'];
        //$items["nickname"]=$user['to_fishNick'];
        $items["sign"]="五花肉网络科技";
        $items["avatar"]=$user['to_logo'];
        $items["state"]="online";
        $items["logout_timestamp"]=0;
        $items["account_state"]="normal";
        $items["is_friend"]="true";
        $items["blocked"]="0";
        $items["remark"]="";
        $items["allow_friend"]=1;
        $items["top"]=0;
    
        $arr["code"] = 0;
        $arr["msg"] = "ok";
        $arr["data"] = $items;
        echo json_encode($arr);

}else if ($s == "/h5/friend/getlist" || $s == "/web/friend/getlist"  ) {
     echo '{"code":0,"msg":"ok","data":{"M":{"1":{"uid":1,"first_char":"M","avatar":"/static/avatar.jpg","state":"offline","name":"五花肉","initials":"M"}},"W":{"88744":{"uid":88744,"first_char":"W","avatar":"/static/h5/img/2260d1666dc2ad.jpg","state":"online","name":"网络科技","initials":"W"}}}}';
    
}else if ($s == "/h5/message/revoke" || $s == "/web/message/revoke"  ) {
    $mid = $_REQUEST["mid"];
    $db->del("im","id=$mid");
    echo '{"code":0,"msg":"ok","data":[]}';
    
}else if ($s == "/h5/pop/delchat" || $s =="/web/pop/delchat" ) {
    $uid = $_REQUEST["id"];
    $db->del("im","sid='$uid'");
    echo '{"code":0,"msg":"ok","send":"","data":{"mid":"17500020","getui":""}}';
    
}else if ($s == "/h5/message/get" || $s =="/web/message/get") {
    //id=88744&type=friend&limit=20
    $uid = $_REQUEST["id"];
    $mid = $_REQUEST["mid"];
    // echo '{"code":0,"msg":"ok","data":[{"mid":17488906,"from":88744,"avatar":"\/static\/avatar.jpg","timestamp":1709121531,"content":"我通过了你的好友请求，我们来聊天吧","sub_type":"message"},{"mid":17488921,"from":89146,"avatar":"\/static\/avatar.jpg","timestamp":1709121578,"content":"0000","sub_type":"message"}]}';
    if($mid){
        $pageSql = " id < " .$mid ." and ";
    }
    
    //$sn_rs = $db->select("im", "sid = '$uid' ORDER BY id DESC","",1,"*","20");
    
    $res =$db->query("(select * from im WHERE $pageSql sid = '$uid' order by id desc limit 0,20) order by id");
    $sn_rs = $db->arrty($res, 1);
    
    // var_dump($sn_rs);
    //{"mid":17488921,"from":89146,"avatar":"/static/avatar.jpg","timestamp":1709121578,"content":"0000","sub_type":"message"}
    foreach ($sn_rs as &$user) {
        $items["avatar"]=$user['mainPic']=="(null)"?"/static/avatar.jpg":$user['mainPic'];
        $items["mid"]=$user['id'];
        //$items["from"]=89146;//$user['to_userId'];
        $items["from"]=$user['to_userId'];
        $items["from"]=$user['sid'];
        
        $items["sub_type"]="message";
        if($user['to_userId'] == 89146){
            $items["avatar"]=$user['to_logo'];
            $items["avatar"]=$user['owner_logo'];
            
            $items["from"]=89146;
        }
        
        if($user['to_userId'] == "100"){
            $items["sub_type"]="notice";
        }
        // $items["to"]=$user['owner_userId'];
        $items["content"]=$user['content'];
        
        if (strpos($user['content'], '4   ') !== false) {  
            //file[IMG_0128_compressMedium.mp4	5.43 MB](http://wantu-xm4-xianyu-video-hz.oss-cn-hangzhou.aliyuncs.com/aus/xianyu_imvod_private/187843300/643DC472AB8643A8962328B6A5D80E7F?video_id=451430442800)
            $originalText = $user['content']; 
            $searchString = "4   "; 
            $replaceString =  '4	';  
            $items["content"] = str_replace($searchString, $replaceString, $originalText); 
        }
        $items["timestamp"]=$user['msgtime'];
        // $items["unread_count"]=0;
        $artdata[]=$items;
    }
    
$arr["code"] = 0;
$arr["msg"] = "ok";
$arr["data"] = $artdata;
echo json_encode($arr);

}else if ($s == "/h5/message/send" || $s == "/web/message/send") {
//     array(5) {
//   ["s"]=>
//   string(16) "/h5/message/send"
//   ["to"]=>
//   string(5) "88744"
//   ["content"]=>
//   string(5) "11111"
//   ["type"]=>
//   string(6) "friend"
//   ["uniqueId"]=>
//   string(13) "1709139545304"
// }

    $sid = $_REQUEST["to"];
    $content = $_REQUEST["content"];
    
    
    $message = urlencode($content);
    $user = $db->select("im", "sid='$sid'");
    
    $owner_userId = $user['owner_userId'];
    //$sid = $user['sid'];
    
    $url = "http://101.42.11.186:6666/GatewayWorker/Applications/YourApp/test.php?message=$message&uid=$owner_userId&sid=$sid";
    $html = file_get_contents($url);
    if($html == '{"code":"0","msg":"success"}'){
        
    }else{
        echo '{"code":2,"msg":'.$html.'}';
        return;
    }
    // var_dump($user);
    
    
                $sid = $user["sid"];
                //$content = $user["content"];89146
                $itemId = $user["itemId"];
                $mainPic = $user["mainPic"];
                $owner_userId = $user["owner_userId"];
                $owner_fishNick = $user["owner_fishNick"];
                $to_userId = 89146;//$user["to_userId"];
                $to_fishNick = $user["to_fishNick"];
                
                 $owner_logo= $user["owner_logo"];
                $to_logo= $user["to_logo"];
                $msgtime = time();
                // 使用数据库实例
                // $ret = self::$db->select('*')->from('im')->where('id > 1')->query();
                // Gateway::sendToClient($client_id, var_export($ret, true));
                
                //$ret = self::$db->query("INSERT INTO `im` ( sid,content,itemId,mainPic,owner_userId,owner_fishNick,to_userId,to_fishNick,owner_logo,to_logo,msgtime )
//VALUES ( '$sid','$content','$itemId','$mainPic','$owner_userId','$owner_fishNick','$to_userId','$to_fishNick','$owner_logo','$to_logo',$msgtime )");

                $db->info("im", "sid,content,itemId,mainPic,owner_userId,owner_fishNick,to_userId,to_fishNick,owner_logo,to_logo,msgtime", "'$sid','$content','$itemId','$mainPic','$owner_userId','$owner_fishNick','$to_userId','$to_fishNick','$owner_logo','$to_logo',$msgtime");
                
    echo '{"code":0,"msg":"ok","send":'.$html.',"data":{"mid":"17500020","getui":""}}';
    
} else if ($s == "/h5/pop/get" || $s =="/web/pop/get") {
    //to_userId != '100' 
    $sn_rs = $db->select("im", "1 GROUP BY sid DESC","",1);
    // $res =$db->query("SELECT * FROM `im` GROUP BY sid");
    // $sn_rs = $db->arrty($res, 1);
    // var_dump($sn_rs);
    //   ["id"]=>
    // string(1) "4"
    // ["sid"]=>
    // string(11) "36324370836"
    // ["content"]=>
    // string(15) "便宜点呗！"
    // ["itemId"]=>
    // string(12) "767485642623"
    // ["mainPic"]=>
    // string(81) "https://img.alicdn.com/bao/uploaded/i1/O1CN01WcGBB327T534p8b8M_!!0-fleamarket.jpg"
    // ["owner_userId"]=>
    // string(13) "2208321587797"
    // ["owner_fishNick"]=>
    // string(12) "小姐姐哟"
    // ["to_userId"]=>
    // string(13) "2217461805005"
    // ["to_fishNick"]=>
    // string(18) "捞鱼我专业的"
    foreach ($sn_rs as &$user) {
        $items["mid"]=$user['id'];
        $items["from"]=$user['to_userId'];
        $items["to"]=$user['owner_userId'];
        $items["content"]=$user['content'];
        $items["timestamp"]=$user['msgtime'];
        $items["sub_type"]="message";
        $items["unread_count"]=0;
        $items["avatar"]=$user['mainPic'];
        
        $friend["type"]="friend";
        // $friend["type"]=$user['owner_userId'];
        $friend["id"]=$user['sid'];
        $friend["avatar"]=$user['to_logo'];
        $friend["name"]="[".$user['owner_fishNick']."]".$user['to_fishNick'];
        $friend["unread_count"]=0;
        //{"mid":17488921,"from":89146,"to":88744,"content":"0000","timestamp":1709121578,"sub_type":"message","unread_count":0,"avatar":"\/static\/avatar.jpg"}
        $friend["items"]=[$items];
        $friend["top"]=0;
        
        $chatting["friend".$user['sid']]=$friend;
    }
$mine["uid"]=89146;
$mine["username"]="admin";
$mine["sign"]="你好，世界。";
$mine["nickname"]="admin";
$mine["avatar"]="/static/avatar.jpg";
$mine["prompt_tone"]="on";
$mine["unread_friend_apply_count"]=0;
$mine["call_sig"]="eJwtzF0LgjAYhuH-suOQd2s6J3QoEfSlfYFnqcteljHmCCP67y318LkeuD-kuD4EL2VJQlgAZDZsrNXT4Q0HjiXl0XR0tb4agzVJKAeIgcl5PD6qN2iV9zAMGQCM6rD9mwBJmc-IqYKN77YCo026p7lY5lUhzttTtssufbaijncFNe-7Q*vSlmnVLMj3B5WbMIE_";
// $mine[""]="";

//{"friend1":{"type":"friend","id":1,"avatar":"\/\/im.popoim.com\/upload\/avatars\/1\/1\/2260d1666dc2ad.jpg","name":"默认好友","unread_count":0,"items":[],"top":0},"friend88744":{"type":"friend","id":88744,"avatar":"\/static\/avatar.jpg","name":"wangxd","unread_count":0,"items":[{"mid":17488921,"from":89146,"to":88744,"content":"0000","timestamp":1709121578,"sub_type":"message","unread_count":0,"avatar":"\/static\/avatar.jpg"}],"top":0}}
// $friend["type"]="friend";
// $friend["id"]=88744;
// $friend["avatar"]="https://img.alicdn.com/bao/uploaded/i1/O1CN01WcGBB327T534p8b8M_!!0-fleamarket.jpg";
// $friend["name"]="默认好友";
// $friend["unread_count"]=0;
// $friend["items"]=[];
// $friend["top"]=0;
// $chatting["friend1"]=$friend;
// $chatting["friend2"]=$friend;

//{"id":1,"voice":"on","audio":"off","video":"off","prompt_tone":"on","group_chat":"on","private_chat":"on","add_friend":"on","create_group":"on","upload_file":"on","upload_img":"on","emoji":"on","member_detail":"on","stranger_chat":"off","appkey":"","ws_address":"wss:\/\/ws.popoim.com\/ws","app_name":"泡泡IM","register":"on","register_captcha":"on","login_captcha":"on","explore_url":"https:\/\/www.workerman.net","qrcode_friend":"on","qrcode_group":"on","qrcode_scan":"on","video_call":"off","call_appid":"1400802938","audio_call":"off"}
$setting["id"] = 1;
$setting["voice"] = "on";
$setting["audio"] = "off";
$setting["video"] = "off";
$setting["prompt_tone"] = "on";
$setting["group_chat"] = "on";
$setting["private_chat"] = "on";
$setting["add_friend"] = "on";
$setting["create_group"] = "on";
$setting["upload_file"] = "on";
$setting["emoji"] = "on";
$setting["member_detail"] = "on";
$setting["stranger_chat"] = "off";
$setting["appkey"] = "";
// $setting["ws_address"] = "wss://ws.popoim.com/ws";
$setting["ws_address"] = "ws://101.42.11.186:8282";
$setting["app_name"] = "泡泡鱼";
$setting["register"] = "on";
$setting["register_captcha"] = "on";
$setting["login_captcha"] = "on";
$setting["explore_url"] = "https://www.workerman.net";
$setting["qrcode_friend"] = "on";
$setting["qrcode_group"] = "on";
$setting["qrcode_scan"] = "on";
$setting["video_call"] = "off";
$setting["call_appid"] = "1400802938";
$setting["audio_call"] = "off";
$artdata["mine"] = $mine;
$artdata["chatting"] = $chatting;
$artdata["friend"] = [];
$artdata["group"] = [];
$artdata["setting"] = $setting;
$arr["code"] = 0;
$arr["msg"] = "ok";
$arr["data"] = $artdata;

// echo '{"code":0,"msg":"ok","data":{"mine":{"uid":89146,"username":"vipbtt","sign":"vipbtt","nickname":"vipbtt","avatar":"\/static\/avatar.jpg","prompt_tone":"on","unread_friend_apply_count":0,"call_sig":"eJwtzF0LgjAYhuH-suOQd2s6J3QoEfSlfYFnqcteljHmCCP67y318LkeuD-kuD4EL2VJQlgAZDZsrNXT4Q0HjiXl0XR0tb4agzVJKAeIgcl5PD6qN2iV9zAMGQCM6rD9mwBJmc-IqYKN77YCo026p7lY5lUhzttTtssufbaijncFNe-7Q*vSlmnVLMj3B5WbMIE_"},"friend":[],"group":[],"chatting":{"friend1":{"type":"friend","id":1,"avatar":"\/\/im.popoim.com\/upload\/avatars\/1\/1\/2260d1666dc2ad.jpg","name":"默认好友","unread_count":0,"items":[],"top":0},"friend88744":{"type":"friend","id":88744,"avatar":"\/static\/avatar.jpg","name":"wangxd","unread_count":0,"items":[{"mid":17488921,"from":89146,"to":88744,"content":"0000","timestamp":1709121578,"sub_type":"message","unread_count":0,"avatar":"\/static\/avatar.jpg"}],"top":0}},"setting":{"id":1,"voice":"on","audio":"off","video":"off","prompt_tone":"on","group_chat":"on","private_chat":"on","add_friend":"on","create_group":"on","upload_file":"on","upload_img":"on","emoji":"on","member_detail":"on","stranger_chat":"off","appkey":"","ws_address":"wss:\/\/ws.popoim.com\/ws","app_name":"泡泡IM","register":"on","register_captcha":"on","login_captcha":"on","explore_url":"https:\/\/www.workerman.net","qrcode_friend":"on","qrcode_group":"on","qrcode_scan":"on","video_call":"off","call_appid":"1400802938","audio_call":"off"}}}';
echo json_encode($arr);
}
else{
var_dump($_REQUEST);
}

?>